<?php

namespace App\Jobs;

use App\Models\Cashbook;
use App\Models\Cashflow;
use App\Models\Category;
use App\Models\Mplogin;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class ClearMemberDataJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $user_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 清理登录
        Mplogin::query()->where('user_id', $this->user_id)->delete();

        // 清理分类
        Category::query()->where('user_id', '<>', 0)->where('user_id', $this->user_id)->delete();

        // 清理账单
        Cashflow::query()->where('user_id', $this->user_id)->delete();

        // 清理账单
        Cashbook::query()->where('user_id', $this->user_id)->delete();

        // 清理关联账本
        DB::table('user_cashbooks')->where('user_id', $this->user_id)->delete();
    }
}
